using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;

using WOODIFramework.Model;

namespace WOODIFramework.Model
    .Helpers
{
    public class DDDPageHelper
    {
        public string BuildPage(string PageName)
        {
            StringBuilder pageText = new StringBuilder();
            Model.Page page = Model.Page.FindPageByPageName(PageName);

            if (page.GetType().ToString() == "WOODIFramework.Model.FormPage")
            { 
                FormPage formPage = (FormPage)page;
                pageText.Append(string.Format("<form action=\"{0}\">", formPage.Action.ToString()));
            }

            pageText.Append(BuildTable(page));

            if (page.GetType().ToString() == "WOODIFramework.Model.FormPage")
            {
                pageText.Append("</form>"); 
            }

            return pageText.ToString();
        }

        private string BuildTable(Model.Page page)
        {
            StringBuilder pageText = new StringBuilder();
            pageText.Append("<table width=\"100%\" border=\"1\">");
            for (int rowNumber = 0; rowNumber < page.NumberRows; rowNumber++)
            {
                pageText.Append("<tr>");
                for (int columnNumber = 0; columnNumber < page.NumberColumns; columnNumber++)
                {
                    pageText.Append("<td>");
                    //pageText.Append(columnNumber.ToString() + ", " + rowNumber.ToString() + " - ");
                    if (page.PageItemsAsTable[columnNumber, rowNumber] != null)
                    {
                        //pageText.Append(((List<Object>)page.PageItemsAsTable[columnNumber, rowNumber]).Count);
                        foreach (Object item in (List<Object>)page.PageItemsAsTable[columnNumber, rowNumber])
                        {
                            pageText.Append(ProcessPageItem(item));
                        }
                    }
                    else
                    { pageText.Append("&nbsp;"); }
                    pageText.Append("</td>");
                }
                pageText.Append("</tr>");
            }

            pageText.Append("</table>");
            return pageText.ToString();
        }

        private string ProcessPageItem(Object pageItem)
        {
            string pageHTML = string.Empty;
            
            switch (pageItem.GetType().ToString())
            {
                case "WOODIFramework.Model.Label":
                    pageHTML = ProcessLabel((Model.Label)pageItem);
                    break;
                case "WOODIFramework.Model.TextBox":
                    pageHTML = ProcessTextBox((Model.TextBox)pageItem);
                    break;
                case "WOODIFramework.Model.SubmitButton":
                    pageHTML = ProcessSubmitButton((Model.SubmitButton)pageItem);
                    break;
            }
            return pageHTML;
        }

        private string ProcessLabel(Model.Label LabelItem)
        {
            string pageHTML = string.Format("<span id=\"{0}\" name=\"{1}\" title=\"{2}\">{3}</span>", LabelItem.Id.ToString(), LabelItem.Name, LabelItem.Tooltip, LabelItem.LabelText);
            return pageHTML;
        }

        private string ProcessTextBox(Model.TextBox TextBoxItem)
        {
            string pageHTML = string.Format("<input type=\"Text\" id=\"{0}\" Name=\"{1}\" Title=\"{2}\" value=\"{3}\"></input>", TextBoxItem.Id.ToString(), TextBoxItem.Name, TextBoxItem.Tooltip, TextBoxItem.TextBoxText);
            return pageHTML;
        }

        private string ProcessSubmitButton(Model.SubmitButton SubmitButtonItem)
        {
            string pageHTML = string.Format("<input type=\"Submit\" id=\"{0}\" Name=\"{1}\" Title=\"{2}\" value=\"{3}\"></input>", SubmitButtonItem.Id.ToString(), SubmitButtonItem.Name, SubmitButtonItem.Tooltip, SubmitButtonItem.ButtonText);
            return pageHTML;
        }
    }
}
